package pre.Work;

import java.util.*;

public class Exam03 implements Runnable {
    @Override
    public void run() {
    }

    //编写一个多线程排序程序，将一个大型数组（或者集合）分成若三部分，创建三个线程每个线程对其中一部
    //分进行排序，然后主线程(main)等待所有线程完成，最后输出这个数组
    public static void main(String[] args) {
        Suiji(2000);
    }

    public static void Suiji(int num) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Random random = new Random();
            int number = random.nextInt(1001);
            list.add(number);
        }
        Thread thread1 = new Thread(() -> {
            Collections.sort(list.subList(0, num / 3));
        });
        Thread thread2 = new Thread(() -> {
            Collections.sort(list.subList(num / 3, 2 * num / 3));
        });

        Thread thread3 = new Thread(() -> {
            Collections.sort(list.subList(2 * num / 3, num));
        });
        thread1.start();
        thread2.start();
        thread3.start();

        try {
            thread1.join();
            thread2.join();
            thread3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list);
    }
}
//    Comparator comparator = new Comparator<Integer>() {
//        @Override
//        public int compare(Integer o1, Integer o2) {
//            return o1 - o2;
//        }
//    };
//        List<Integer> list1 = new ArrayList<>(list.subList(0, num / 3));
//        Thread thread1 = new Thread(() -> list1.sort(comparator));
//        System.out.println("集合：" + list1);
//
//        List<Integer> list2 = new ArrayList<>(list.subList(num / 3, 2 * num / 3));
//        Thread thread2 = new Thread(() -> list2.sort(comparator));
//        System.out.println("集合：" + list2);
//
//        List<Integer> list3 = new ArrayList<>(list.subList(2 * num / 3, num));
//        Thread thread3 = new Thread(() -> list3.sort(comparator));
//        System.out.println("集合：" + list3);
//
//        thread1.start();
//        thread2.start();
//        thread3.start();
//
//        try {
//            thread1.join();
//            thread2.join();
//            thread3.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        List<Integer> sortedList = new ArrayList<>();
//        sortedList.addAll(list1);
//        sortedList.addAll(list2);
//        sortedList.addAll(list3);
//
//        System.out.println(sortedList);

